using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
namespace MicroMsg.Manager
{
	public static class HDheadImgMgr
	{
		private const string TAG = "HDheadImgMgr";
		private static string strUsrNameDownLoading;
		private static onGetHDheadImgCallback callBack;
		private static Dictionary<string, bool> mapHDheadImg;
		private static onUploadHDheadImgCallback uploadCallBack;
		static HDheadImgMgr()
		{
			HDheadImgMgr.mapHDheadImg = new Dictionary<string, bool>();
			EventCenter.registerEventHandler(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_SUCCESS, new EventHandlerDelegate(HDheadImgMgr.onGetImage));
			EventCenter.registerEventHandler(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR, new EventHandlerDelegate(HDheadImgMgr.onGetImage));
			EventCenter.registerEventHandler(EventConst.ON_NETSCENE_UPLOAD_HEADIMG_SUCCESS, new EventHandlerDelegate(HDheadImgMgr.onUploadImage));
			EventCenter.registerEventHandler(EventConst.ON_NETSCENE_UPLOAD_HEADIMG_ERR, new EventHandlerDelegate(HDheadImgMgr.onUploadImage));
		}
		public static void uninit()
		{
		}
		private static void onGetImage(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			HDheadImgMgr.doGetHeadImageCallBack(evtArgs.mEventID == EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_SUCCESS);
		}
		private static void doGetHeadImageCallBack(bool bSuccess)
		{
			if (HDheadImgMgr.callBack == null)
			{
				return;
			}
			onGetHDheadImgCallback onGetHDheadImgCallback = HDheadImgMgr.callBack;
			HDheadImgMgr.callBack = null;
			string text = HDheadImgMgr.strUsrNameDownLoading;
			HDheadImgMgr.strUsrNameDownLoading = null;
			if (!bSuccess)
			{
				Log.smethod_4("HDheadImgMgr", "sceneDownloadHDHeadImg fail " + text);
				onGetHDheadImgCallback(false, text, null);
				return;
			}
			Log.smethod_4("HDheadImgMgr", "sceneDownloadHDHeadImg success " + text);
			BitmapImage bitmapImage = ImageCacheMgr.loadFromFile(StorageMgr.hdheadimg.getPath(text));
			if (bitmapImage == null)
			{
				Log.smethod_4("HDheadImgMgr", "sceneDownloadHDHeadImg decode heaimage fail " + text);
				onGetHDheadImgCallback(false, text, null);
				return;
			}
			onGetHDheadImgCallback(true, text, bitmapImage);
		}
		public static bool hasHDheadImg(BottleContact bottleContact_0)
		{
			return bottleContact_0 != null && bottleContact_0.nHDImgFlag != 0;
		}
		public static void setHasHDheadImg(string usrName, bool bHasHDheadImg)
		{
			if (string.IsNullOrEmpty(usrName))
			{
				return;
			}
			HDheadImgMgr.mapHDheadImg.set_Item(usrName, bHasHDheadImg);
		}
		public static bool hasHDheadImg(string usrName)
		{
			if (string.IsNullOrEmpty(usrName))
			{
				return false;
			}
			if (usrName == AccountMgr.strUsrName + "@bottle")
			{
				return true;
			}
			if (HDheadImgMgr.mapHDheadImg.ContainsKey(usrName))
			{
				return HDheadImgMgr.mapHDheadImg.get_Item(usrName);
			}
			if (usrName == AccountMgr.strUsrName + "@bottle")
			{
				return StorageMgr.hdheadimg.exist(usrName);
			}
			Contact contact = StorageMgr.contact.method_0(usrName);
			return contact != null && contact.nHasWeiXinHdHeadImg != 0u;
		}
		public static BitmapImage getHeadImage(string usrName)
		{
			if (string.IsNullOrEmpty(usrName))
			{
				return null;
			}
			if (!StorageMgr.hdheadimg.exist(usrName))
			{
				return null;
			}
			BitmapImage bitmapImage = ImageCacheMgr.loadFromFile(StorageMgr.hdheadimg.getPath(usrName));
			if (bitmapImage == null)
			{
				Log.smethod_4("HDheadImgMgr", "sceneDownloadHDHeadImg decode heaimage fail " + usrName);
				return HeadImgMgr.defaultHeadImage;
			}
			return bitmapImage;
		}
		public static bool getHeadImage(string usrName, onGetHDheadImgCallback onGetHDheadImgCallback_0)
		{
			if (string.IsNullOrEmpty(usrName))
			{
				return false;
			}
			if (HDheadImgMgr.strUsrNameDownLoading != null)
			{
				return false;
			}
			if (StorageMgr.hdheadimg.exist(usrName))
			{
				Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
				{
					HDheadImgMgr.doGetHeadImageCallBack(true);
				});
				HDheadImgMgr.callBack = onGetHDheadImgCallback_0;
				HDheadImgMgr.strUsrNameDownLoading = usrName;
				return true;
			}
			bool flag = ContactHelper.isBottleContact(usrName);
			if (!ServiceCenter.sceneDownloadHDHeadImg.doScene(usrName, flag ? 2 : 1))
			{
				Log.smethod_5("HDheadImgMgr", "sceneDownloadHDHeadImg fail");
				return false;
			}
			HDheadImgMgr.callBack = onGetHDheadImgCallback_0;
			HDheadImgMgr.strUsrNameDownLoading = usrName;
			return true;
		}
		private static void onUploadImage(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (HDheadImgMgr.uploadCallBack == null)
			{
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_UPLOAD_HEADIMG_SUCCESS)
			{
				HDheadImgMgr.uploadCallBack(true);
				GuideHeadSetMgr.deleteGuideHeadItem();
			}
			else
			{
				HDheadImgMgr.uploadCallBack(false);
			}
			HDheadImgMgr.uploadCallBack = null;
		}
		public static bool uploadHDheadimg(Stream fileStream, onUploadHDheadImgCallback onUploadHDheadImgCallback_0)
		{
			HDheadImgMgr.uploadCallBack = onUploadHDheadImgCallback_0;
			return ServiceCenter.sceneUploadHDHeadImg.doScene(fileStream, 1);
		}
		public static bool uploadDriftBottleImg(Stream fileStream, onUploadHDheadImgCallback onUploadHDheadImgCallback_0)
		{
			HDheadImgMgr.uploadCallBack = onUploadHDheadImgCallback_0;
			return ServiceCenter.sceneUploadHDHeadImg.doScene(fileStream, 2);
		}
		public static bool delHeadimg(string userName)
		{
			if (string.IsNullOrEmpty(userName))
			{
				return false;
			}
			StorageIO.deleteFile(StorageMgr.hdheadimg.getPath(userName));
			return true;
		}
	}
}
